<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="分析结果编号" prop="offlineResultNo">
        <el-input
          v-model="queryParams.offlineResultNo"
          placeholder="请输入分析结果编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="部门名称" prop="departmentName">
        <el-input
          v-model="queryParams.departmentName"
          placeholder="请输入部门名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仪器名称" prop="instrumentName">
        <el-input
          v-model="queryParams.instrumentName"
          placeholder="请输入仪器名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模型编号" prop="modelNo">
        <el-input
          v-model="queryParams.modelNo"
          placeholder="请输入模型编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采样时间" prop="samplingTime">
        <el-date-picker
          clearable
          v-model="queryParams.samplingTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择采样时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['analysis:offline:add']"
          >新增离线分析</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['analysis:offline:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['analysis:offline:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['analysis:offline:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="offlineList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="分析结果编号"
        align="center"
        prop="offlineResultNo"
      />
      <el-table-column label="成分名称" align="center" prop="constituentName" />
      <el-table-column label="预测值" align="center" prop="predicted" />
      <el-table-column label="部门名称" align="center" prop="departmentName" />
      <el-table-column label="仪器编号" align="center" prop="instrumentNo" />
      <el-table-column label="仪器名称" align="center" prop="instrumentName" />
      <el-table-column label="模型编号" align="center" prop="modelNo" />
      <el-table-column
        label="采样时间"
        align="center"
        prop="samplingTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.samplingTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['analysis:offline:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['analysis:offline:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改离线分析历史对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分析结果编号" prop="offlineResultNo">
          <el-input
            v-model="form.offlineResultNo"
            placeholder="请输入分析结果编号"
          />
        </el-form-item>
        <el-form-item label="成分id" prop="constituentId">
          <el-input v-model="form.constituentId" placeholder="请输入成分id" />
        </el-form-item>
        <el-form-item label="成分名称" prop="constituentName">
          <el-input
            v-model="form.constituentName"
            placeholder="请输入成分名称"
          />
        </el-form-item>
        <el-form-item label="预测值" prop="predicted">
          <el-input v-model="form.predicted" placeholder="请输入预测值" />
        </el-form-item>
        <el-form-item label="部门名称" prop="departmentName">
          <el-input
            v-model="form.departmentName"
            placeholder="请输入部门名称"
          />
        </el-form-item>
        <el-form-item label="部门id" prop="departmentId">
          <el-input v-model="form.departmentId" placeholder="请输入部门id" />
        </el-form-item>
        <el-form-item label="仪器id" prop="instrumentId">
          <el-input v-model="form.instrumentId" placeholder="请输入仪器id" />
        </el-form-item>
        <el-form-item label="仪器编号" prop="instrumentNo">
          <el-input v-model="form.instrumentNo" placeholder="请输入仪器编号" />
        </el-form-item>
        <el-form-item label="仪器名称" prop="instrumentName">
          <el-input
            v-model="form.instrumentName"
            placeholder="请输入仪器名称"
          />
        </el-form-item>
        <el-form-item label="模型id" prop="modelId">
          <el-input v-model="form.modelId" placeholder="请输入模型id" />
        </el-form-item>
        <el-form-item label="模型编号" prop="modelNo">
          <el-input v-model="form.modelNo" placeholder="请输入模型编号" />
        </el-form-item>
        <el-form-item label="采样时间" prop="samplingTime">
          <el-date-picker
            clearable
            v-model="form.samplingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择采样时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input
            v-model="form.remarks"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="是否删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入是否删除" />
        </el-form-item>
        <el-form-item label="新增人id" prop="creatorId">
          <el-input v-model="form.creatorId" placeholder="请输入新增人id" />
        </el-form-item>
        <el-form-item label="新增人" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入新增人" />
        </el-form-item>
        <el-form-item label="修改人id" prop="modifierId">
          <el-input v-model="form.modifierId" placeholder="请输入修改人id" />
        </el-form-item>
        <el-form-item label="修改人" prop="modifier">
          <el-input v-model="form.modifier" placeholder="请输入修改人" />
        </el-form-item>
        <el-form-item label="修改时间" prop="modifyTime">
          <el-date-picker
            clearable
            v-model="form.modifyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择修改时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="样品光谱id" prop="sampleSpectrumId">
          <el-input
            v-model="form.sampleSpectrumId"
            placeholder="请输入样品光谱id"
          />
        </el-form-item>
        <el-form-item label="样品光谱编号" prop="sampleSpectrumNo">
          <el-input
            v-model="form.sampleSpectrumNo"
            placeholder="请输入样品光谱编号"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOffline,
  getOffline,
  delOffline,
  addOffline,
  updateOffline,
} from "@/api/analysis/offline";

export default {
  name: "Offline",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 离线分析历史表格数据
      offlineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        offlineResultNo: null,
        constituentId: null,
        constituentName: null,
        predicted: null,
        departmentName: null,
        departmentId: null,
        instrumentId: null,
        instrumentNo: null,
        instrumentName: null,
        modelId: null,
        modelNo: null,
        samplingTime: null,
        remarks: null,
        isDelete: null,
        creatorId: null,
        creator: null,
        modifierId: null,
        modifier: null,
        modifyTime: null,
        sampleSpectrumId: null,
        sampleSpectrumNo: null,
        envSpectrumId: null,
        envSpectrumNo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        offlineResultNo: [
          { required: true, message: "分析结果编号不能为空", trigger: "blur" },
        ],
        constituentId: [
          { required: true, message: "成分id不能为空", trigger: "blur" },
        ],
        constituentName: [
          { required: true, message: "成分名称不能为空", trigger: "blur" },
        ],
        predicted: [
          { required: true, message: "预测值不能为空", trigger: "blur" },
        ],
        departmentName: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
        ],
        departmentId: [
          { required: true, message: "部门id不能为空", trigger: "blur" },
        ],
        instrumentId: [
          { required: true, message: "仪器id不能为空", trigger: "blur" },
        ],
        instrumentNo: [
          { required: true, message: "仪器编号不能为空", trigger: "blur" },
        ],
        instrumentName: [
          { required: true, message: "仪器名称不能为空", trigger: "blur" },
        ],
        modelId: [
          { required: true, message: "模型id不能为空", trigger: "blur" },
        ],
        modelNo: [
          { required: true, message: "模型编号不能为空", trigger: "blur" },
        ],
        samplingTime: [
          { required: true, message: "采样时间不能为空", trigger: "blur" },
        ],
        isDelete: [
          { required: true, message: "是否删除不能为空", trigger: "blur" },
        ],
        creatorId: [
          { required: true, message: "新增人id不能为空", trigger: "blur" },
        ],
        creator: [
          { required: true, message: "新增人不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "新增时间不能为空", trigger: "blur" },
        ],
        sampleSpectrumId: [
          { required: true, message: "样品光谱id不能为空", trigger: "blur" },
        ],
        sampleSpectrumNo: [
          { required: true, message: "样品光谱编号不能为空", trigger: "blur" },
        ],
        envSpectrumId: [
          { required: true, message: "光谱id不能为空", trigger: "blur" },
        ],
        envSpectrumNo: [
          { required: true, message: "样品光谱编号不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询离线分析历史列表 */
    getList() {
      this.loading = true;
      listOffline(this.queryParams).then((response) => {
        this.offlineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        offlineResultId: null,
        offlineResultNo: null,
        constituentId: null,
        constituentName: null,
        predicted: null,
        departmentName: null,
        departmentId: null,
        instrumentId: null,
        instrumentNo: null,
        instrumentName: null,
        modelId: null,
        modelNo: null,
        samplingTime: null,
        remarks: null,
        isDelete: null,
        creatorId: null,
        creator: null,
        createTime: null,
        modifierId: null,
        modifier: null,
        modifyTime: null,
        sampleSpectrumId: null,
        sampleSpectrumNo: null,
        envSpectrumId: null,
        envSpectrumNo: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.offlineResultId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.open = true;
      this.$router.push("/offline_add");
      this.title = "添加离线分析历史";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const offlineResultId = row.offlineResultId || this.ids;
      getOffline(offlineResultId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改离线分析历史";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.offlineResultId != null) {
            updateOffline(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOffline(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const offlineResultIds = row.offlineResultId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除离线分析历史编号为"' + offlineResultIds + '"的数据项？'
        )
        .then(function () {
          return delOffline(offlineResultIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "analysis/offline/export",
        {
          ...this.queryParams,
        },
        `offline_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
